package org.hdax.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.hdax.entity.Bill;
import org.hdax.entity.IdsModal;
import org.hdax.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String toAdd(){
        return "bill/add";
    }

    @RequestMapping("index")
    public String index(){
        return "bill/list";
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "0")int offset, @RequestParam(defaultValue = "10")int limit,Bill condition){
//        System.out.println("==================================");
//        System.out.println(condition.getBillCode());
//        System.out.println(condition.getProductName());
//        System.out.println("==================================");
        List<Bill> list = billService.getByPage(offset,limit,condition);
        Map<String,Object> map = new HashMap<String, Object>();
        Integer count = billService.getCountAndC(condition);
        map.put("total",count);
        map.put("rows",list);
        return map;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(@RequestParam("id")Integer id){
        Map<String,Object> map = new HashMap<String, Object>();
        boolean isDelete = billService.deleteById(id);
        if(isDelete) map.put("result",true);
        else map.put("result",false);
        return map;
    }
    @RequestMapping(value = "/batchDel",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> batchDelete(IdsModal modal){
        Map<String,Object> map = new HashMap<String, Object>();
        boolean isDelete = billService.batchDelete(modal.getIds());
//        //fastjson json-->string  string-->json
//        JSONArray array = JSON.parseArray(ids);
//        for(int i = 0;i<array.size();i++){
//            //System.out.println(array.get(i));
//            billService.deleteById(Integer.valueOf(array.get(i).toString()));
//        }
//        map.put("result",true);
        //        boolean isDelete = billService.batchDelete(ids);
//        if(isDelete) map.put("result",true);
//        else map.put("result",false);
        if(isDelete) map.put("result",true);
        else map.put("result",false);
        return map;
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doAdd(Bill bill){
        Map<String,Object> map = new HashMap<String, Object>();
        boolean isSave = billService.saveBill(bill);
        if(isSave) map.put("result",true);
        else map.put("result",false);
        return map;
    }

    @RequestMapping(value = "edit/{id}",method = RequestMethod.GET)
    public String toEdit(@PathVariable("id")int id,HttpServletRequest request){
        Bill bill = billService.getById(id);
        if(bill!=null){
            request.setAttribute("bill",bill);
        }
        return "bill/edit";
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doEdit(Bill bill){
        Map<String,Object> map = new HashMap<String, Object>();
        boolean isUpdate = billService.updateBill(bill);
        if(isUpdate) map.put("result",true);
        else map.put("result",false);
        return map;
    }
}
